/*
 * Copyright 2011 Greg Milette and Adam Stroud
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.upb.acs.jobsmaps.voice;

import java.util.*;

public class WordMatcher{
    private Set<String> words;
    public static final int NOT_IN = -1;

    public WordMatcher(String... wordsIn)
    {
        this(Arrays.asList(wordsIn));
    }

    public WordMatcher(List<String> wordsIn)
    {
        //care about order so we can execute isInAt
        words = new LinkedHashSet<String>(wordsIn);
    }

    public Set<String> getWords()
    {
        return words;
    }

	public String isIn(String word, Set<String> words)
	{
		for (String s : words) {
			if (s.equalsIgnoreCase(word)){
				return s;
			}
			if (s.startsWith(word)){
				return s;
			}
			if (s.endsWith(word)){
				return s;
			}

		}
		return null;
	}

    public String isIn(String [] wordsIn)
    {
        boolean wordIn = false;
        for (String word : wordsIn)
        {
			String in = isIn(word, words);
			if (in != null)
            {
				return in;
            }
        }
		for (String s : words) {
			String in = isIn(s, new HashSet<>(Arrays.asList(wordsIn)));
			if (in != null)
			{
				return in;
			}
		}
		return null;
    }

    public int isInAt(String wordCheck)
    {
        int which = NOT_IN;
        int ct = 0;
        for (String word : words)
        {
            if (word.equals(wordCheck))
            {
                which = ct;
                break;
            }
            ct++;
        }
        return which;
    }

    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder();
        for (String word : getWords())
        {
            sb.append(word).append(" ");
        }
        return sb.toString().trim();
    }
}
